package com.hanrui.android.net.presenter.login;


import com.hanrui.android.library.base.BaseBean;
import com.hanrui.android.library.base.BasePresenter;
import com.hanrui.android.constant.Constant;
import com.hanrui.android.model.User;
import com.hanrui.android.library.net.ApiService;
import com.hanrui.android.library.net.RetrofitManager;
import com.hanrui.android.library.net.RxSchedulers;
import com.hanrui.android.net.contract.login.LoginContract;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 * 登录
 * Created by Administrator on 2018/3/9.
 */

public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter {

    @Inject
    public LoginPresenter(){}

    @Override
    public void login(String username, String password) {
        mView.showLoading();
        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .login(username,password)
                .compose(RxSchedulers.<BaseBean<User>>applySchedulers())
                .compose(mView.<BaseBean<User>>bindToLife())
                .subscribe(new Consumer<BaseBean<User>>() {
                    @Override
                    public void accept(BaseBean<User> response) throws Exception {
                        mView.hideLoading();
                        if (response.getCode() == 1) {
                            mView.loginSuccess(response.getData());
                        } else {
                            mView.showFaild(String.valueOf(response.getMessage()));
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.hideLoading();
                        mView.showFaild(throwable.getMessage());
                    }
                });
    }
}
